//
//  HWMeHeadView.m
//  嗨文
//
//  Created by 响当当 on 2019/1/10.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMeHeadView.h"
#import "HWReChargeViewController.h"
#import "HWPersonInformationViewController.h"
#import "HWRechargeVIPViewController.h"
#import "HWMyWalletViewController.h"
#import "HWBookTicketViewController.h"
#import <ZLPhotoBrowser/ZLPhotoBrowser.h>

@interface HWMeHeadView()
@property (nonatomic, strong)UIImageView *headBackImageView;//背景
@property (nonatomic, strong)UIButton *headButton;//头像
@property (nonatomic, strong)UIButton *hwFriendButton;//嗨书友
@property (nonatomic, strong)UIImageView *hwVipImageView;//VIP
@property (nonatomic, strong)UIButton *hwLevelButton;//嗨等级
@property (nonatomic, strong)UILabel *hwUserNameLabel;//嗨用户名字
@property (nonatomic, strong)UILabel *hwUserIdLabel;//用户id
@property (nonatomic, strong)UIButton *rowButton;//箭头

@property (nonatomic, strong)UILabel *hwBookCoinLabel;//书币
@property (nonatomic, strong)UILabel *hwGoldenCoinLabel;//金钱币


@property (nonatomic, strong)UIButton *hwOpenVipButton;//开通会员按钮
@property (nonatomic, strong)UIImageView *hwNormalImageView;//没有开通vip
@property (nonatomic, strong)UILabel *hwEndTimeLabel;//截止日期

@property (nonatomic, strong)UIImageView *hwAdvertisementImageView;//广告

@property (nonatomic, strong)NSMutableArray *functionArr;//功能按钮数组

@property (nonatomic, strong)UIView *hideView;//背景
@end

@implementation HWMeHeadView

- (UIView *)hideView
{
    if (!_hideView)
    {
        _hideView = [[UIView alloc]initWithFrame:CGRectMake(50*kUIScaleBasePlus,93*kUIScaleBasePlus,180*kUIScaleBasePlus,60*kUIScaleBasePlus)];
        _hideView.backgroundColor = [UIColor whiteColor];
    }
    return _hideView;
}

- (NSMutableArray *)functionArr
{
    if (!_functionArr)
    {
        _functionArr = [[NSMutableArray alloc]init];
    }
    return _functionArr;
}

- (UIImageView *)headBackImageView
{
    if (!_headBackImageView)
    {
        _headBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,170*kUIScaleBasePlus)];
        _headBackImageView.image = [UIImage imageNamed:@"personal_bg"];
    }
    return _headBackImageView;
}

- (UIButton *)headButton
{
    if (!_headButton)
    {
        _headButton = [HWUtilties creatButtonWithFrame:CGRectMake(17*kUIScaleBasePlus,21*kUIScaleBasePlus,33*kUIScaleBasePlus,33*kUIScaleBasePlus)
                                          textAligment:NSTextAlignmentCenter
                                                  text:nil
                                              textClor:nil
                                             buttonBGC:[UIColor clearColor]
                                              textFont:0
                                                target:self
                                                method:@selector(headImageClick)];
        _headButton.layer.masksToBounds = YES;
        _headButton.layer.cornerRadius = _headButton.frame.size.width/2;
        _headButton.layer.borderWidth = 1.5;
        _headButton.layer.borderColor = LCHexColor(0xFFCECB).CGColor;
        [_headButton setBackgroundImage:[UIImage imageNamed:@"head_normal"] forState:UIControlStateNormal];
    }
    return _headButton;
}

- (UILabel *)hwUserNameLabel
{
    if (!_hwUserNameLabel)
    {
        _hwUserNameLabel = [HWUtilties creatLabelWithFrame:CGRectMake(68*kUIScaleBasePlus,21*kUIScaleBasePlus,0,16*kUIScaleBasePlus)
                                              textAligment:NSTextAlignmentLeft textClor:[UIColor whiteColor]
                                                  textFont:16*kUIScaleBasePlus];
        
        
    }
    return _hwUserNameLabel;
}

- (UIButton *)hwFriendButton
{
    if (!_hwFriendButton)
    {
        _hwFriendButton = [HWUtilties creatButtonWithFrame:CGRectZero
                                              textAligment:NSTextAlignmentLeft
                                                      text:nil
                                                  textClor:nil
                                                 buttonBGC:[UIColor clearColor]
                                                  textFont:0
                                                    target:self
                                                    method:@selector(gotoFriend)];
        [_hwFriendButton setBackgroundImage:[UIImage imageNamed:@"book_friend"] forState:UIControlStateNormal];
    }
    return _hwFriendButton;
}

- (UIImageView *)hwVipImageView
{
    if (!_hwVipImageView)
    {
        _hwVipImageView = [[UIImageView alloc]initWithFrame:CGRectMake(226*kUIScaleBasePlus,24*kUIScaleBasePlus,27*kUIScaleBasePlus,11*kUIScaleBasePlus)];
        _hwVipImageView.image = [UIImage imageNamed:@"vip_nactive"];
    }
    return _hwVipImageView;
}

- (UIButton *)hwLevelButton
{
    if (!_hwLevelButton)
    {
        _hwLevelButton = [HWUtilties creatButtonWithFrame:CGRectZero
                                             textAligment:NSTextAlignmentRight
                                                     text:@"嗨赚Lv32"
                                                 textClor:[UIColor whiteColor]
                                                buttonBGC:[UIColor clearColor]
                                                 textFont:12*kUIScaleBasePlus
                                                   target:self
                                                   method:@selector(getMoneyClick)];
        [_hwLevelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:9*kUIScaleBasePlus]];
        [_hwLevelButton setBackgroundImage:[UIImage imageNamed:@"earn_level"] forState:UIControlStateNormal];
        [_hwLevelButton setTitleEdgeInsets:UIEdgeInsetsMake(3,0,0,-15)];
    }
    return _hwLevelButton;
}

- (UILabel *)hwUserIdLabel
{
    if (!_hwUserIdLabel)
    {
        _hwUserIdLabel = [HWUtilties creatLabelWithFrame:CGRectMake(68*kUIScaleBasePlus,CGRectGetMaxY(self.hwUserNameLabel.frame)+9*kUIScaleBasePlus,0,12*kUIScaleBasePlus)
                                            textAligment:NSTextAlignmentLeft
                                                textClor:LCHexColor(0xFFCECB)
                                                textFont:12*kUIScaleBasePlus];
    }
    return _hwUserIdLabel;
}

- (UIButton *)rowButton
{
    if (!_rowButton)
    {
        _rowButton = [HWUtilties creatButtonWithFrame:CGRectMake(343*kUIScaleBasePlus,31*kUIScaleBasePlus,18*kUIScaleBasePlus,23*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentCenter
                                                 text:@""
                                             textClor:nil
                                            buttonBGC:[UIColor clearColor]
                                             textFont:0
                                               target:self
                                               method:@selector(gotoPersonInfo)];
        [_rowButton setBackgroundImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
        
    }
    return _rowButton;
}

- (UILabel *)hwGoldenCoinLabel
{
    if (!_hwGoldenCoinLabel)
    {
        _hwGoldenCoinLabel = [HWUtilties creatLabelWithFrame:CGRectMake(116*kUIScaleBasePlus,93*kUIScaleBasePlus,128*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                    textClor:LCHexColor(0x000000)
                                                    textFont:18*kUIScaleBasePlus];
        
        UILabel *goldenNameLabel = [HWUtilties creatLabelWithFrame:CGRectMake(155*kUIScaleBasePlus,117*kUIScaleBasePlus,50*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                                      textAligment:NSTextAlignmentCenter
                                                          textClor:LCHexColor(0x545454)
                                                          textFont:12*kUIScaleBasePlus];
        goldenNameLabel.text = @"金币钱包";
        [self addSubview:goldenNameLabel];
        
        UIButton *hwGetMoneyButton = [HWUtilties creatButtonWithFrame:CGRectMake(162*kUIScaleBasePlus,134*kUIScaleBasePlus,34*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                                         textAligment:NSTextAlignmentCenter
                                                                 text:@"提现"
                                                             textClor:[UIColor whiteColor]
                                                            buttonBGC:LCHexColor(0xF54337)
                                                             textFont:11*kUIScaleBasePlus
                                                               target:self
                                                               method:@selector(getMoney)];
        hwGetMoneyButton.layer.masksToBounds = YES;
        hwGetMoneyButton.layer.cornerRadius = 4;
        [self addSubview:hwGetMoneyButton];
        
    }
    return _hwGoldenCoinLabel;
}


- (UILabel *)hwBookCoinLabel
{
    if (!_hwBookCoinLabel)
    {
        _hwBookCoinLabel = [HWUtilties creatLabelWithFrame:CGRectMake(17*kUIScaleBasePlus,93*kUIScaleBasePlus,121*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                              textAligment:NSTextAlignmentCenter
                                                  textClor:LCHexColor(0x000000)
                                                  textFont:18*kUIScaleBasePlus];
        
        UILabel *bookCoinLabel = [HWUtilties creatLabelWithFrame:CGRectMake(53*kUIScaleBasePlus,117*kUIScaleBasePlus,47*kUIScaleBasePlus,12*kUIScaleBasePlus)
                                                           textAligment:NSTextAlignmentCenter
                                                               textClor:LCHexColor(0x545454)
                                                               textFont:12*kUIScaleBasePlus];
        bookCoinLabel.text = @"书币";
        [self addSubview:bookCoinLabel];
        
        UIButton *hwTopUpButton = [HWUtilties creatButtonWithFrame:CGRectMake(61*kUIScaleBasePlus,134*kUIScaleBasePlus,34*kUIScaleBasePlus,18*kUIScaleBasePlus)
                                                      textAligment:NSTextAlignmentCenter
                                                              text:@"充值"
                                                          textClor:[UIColor whiteColor]
                                                         buttonBGC:LCHexColor(0xF54337)
                                                          textFont:11*kUIScaleBasePlus
                                                            target:self
                                                            method:@selector(topUpMoney)];
        hwTopUpButton.layer.masksToBounds = YES;
        hwTopUpButton.layer.cornerRadius = 4;
        [self addSubview:hwTopUpButton];

    }
    return _hwBookCoinLabel;
}


- (UILabel *)hwEndTimeLabel
{
    if (!_hwEndTimeLabel)
    {
        UILabel *vipTitle = [HWUtilties creatLabelWithFrame:CGRectMake(244*kUIScaleBasePlus,91*kUIScaleBasePlus,117*kUIScaleBasePlus, 17*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                   textClor:LCHexColor(0x6A543A)
                                                   textFont:18*kUIScaleBasePlus];
        vipTitle.font = [UIFont boldSystemFontOfSize:18*kUIScaleBasePlus];
        vipTitle.text = @"嗨读VIP会员";
        [self addSubview:vipTitle];
        
        _hwEndTimeLabel = [HWUtilties creatLabelWithFrame:CGRectMake(244*kUIScaleBasePlus,CGRectGetMaxY(vipTitle.frame)+4*kUIScaleBasePlus,117*kUIScaleBasePlus, 28*kUIScaleBasePlus)
                                             textAligment:NSTextAlignmentCenter
                                                 textClor:LCHexColor(0x6A543A)
                                                 textFont:10*kUIScaleBasePlus];
        _hwEndTimeLabel.numberOfLines = 0;
        
        UILabel *addVipLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,0,40*kUIScaleBasePlus,15*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentCenter
                                                      textClor:[UIColor whiteColor]
                                                      textFont:9*kUIScaleBasePlus];
        addVipLabel.centerX = _hwEndTimeLabel.centerX;
        addVipLabel.centerY = _hwEndTimeLabel.centerY+25*kUIScaleBasePlus;
        addVipLabel.backgroundColor = LCHexColor(0xF54337);
        addVipLabel.layer.masksToBounds = YES;
        addVipLabel.text = @"续费";
        addVipLabel.layer.cornerRadius = addVipLabel.frame.size.height/2;
        [self addSubview:addVipLabel];
       
    }
    return _hwEndTimeLabel;
}

- (UIImageView *)hwNormalImageView
{
    if (!_hwNormalImageView)
    {
        _hwNormalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(244*kUIScaleBasePlus,75*kUIScaleBasePlus,116*kUIScaleBasePlus,88*kUIScaleBasePlus)];
        
    }
    return _hwNormalImageView;
}

- (UIButton *)hwOpenVipButton
{
    if (!_hwOpenVipButton)
    {
        _hwOpenVipButton = [HWUtilties creatButtonWithFrame:CGRectMake(244*kUIScaleBasePlus,75*kUIScaleBasePlus,116*kUIScaleBasePlus,88*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentCenter
                                                       text:nil
                                                   textClor:nil
                                                  buttonBGC:nil
                                                   textFont:0
                                                     target:self
                                                     method:@selector(openVip)];
    }
    return _hwOpenVipButton;
}

- (UIImageView *)hwAdvertisementImageView
{
    if (!_hwAdvertisementImageView)
    {
        _hwAdvertisementImageView = [[UIImageView alloc]initWithFrame:CGRectMake(18*kUIScaleBasePlus,CGRectGetMaxY(self.headBackImageView.frame)+92*kUIScaleBasePlus,339*kUIScaleBasePlus,56*kUIScaleBasePlus)];
    }
    return _hwAdvertisementImageView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.headBackImageView];
        [self addSubview:self.headButton];
        [self addSubview:self.hwUserNameLabel];
        [self addSubview:self.hwFriendButton];
        [self addSubview:self.hwVipImageView];
        [self addSubview:self.hwLevelButton];
        [self addSubview:self.hwUserIdLabel];
        [self addSubview:self.rowButton];
        [self addSubview:self.hwBookCoinLabel];
        [self addSubview:self.hwGoldenCoinLabel];
        [self addSubview:self.hwEndTimeLabel];
        [self addSubview:self.hwNormalImageView];
        [self addSubview:self.hwOpenVipButton];
        [self addSubview:self.hideView];
        [self creatFunction];
//        [self addSubview:self.hwAdvertisementImageView];
        UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(15*kUIScaleBasePlus,self.frame.size.height-0.5*kUIScaleBasePlus,SCREEN_WIDTH-30*kUIScaleBasePlus,0.5*kUIScaleBasePlus)];
        lineview.backgroundColor = JSALineColor;
        [self addSubview:lineview];
    }
    return self;
}

#pragma mark - 功能 我的书架 阅读记录 邀请好友 我的钱包
- (void)creatFunction
{
    CGFloat width = SCREEN_WIDTH/4;
    CGFloat height = 60*kUIScaleBasePlus;

    NSArray *titlearr = [[NSArray alloc]init];
    
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        titlearr = @[@"我的书架",@"阅读记录",@"阅读口味",@"我的钱包"];
    }
    else
    {
        titlearr = @[@"我的书架",@"阅读记录",@"阅读口味"];
    }
    
    NSArray *arr = @[@"book_shelf",@"read_record",@"invit_friend",@"my_wallet"];
    
    for (int i=0; i<titlearr.count; i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(i*width,CGRectGetMaxY(self.headBackImageView.frame)+10*kUIScaleBasePlus,width,height)
                                            textAligment:NSTextAlignmentCenter
                                                    text:titlearr[i]
                                                textClor:LCHexColor(0x000000)
                                               buttonBGC:[UIColor clearColor]
                                                textFont:12*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(functionClick:)];
        btn.tag = i;
        [btn setImage:[UIImage imageNamed:arr[i]] forState:UIControlStateNormal];
        [btn setUpImageAndDownLableWithSpace:10];
        [_functionArr addObject:btn];
        [self addSubview:btn];
    }
    
}

#pragma mark - 功能选择后跳转
- (void)functionClick:(UIButton *)sender
{
    if (self.gotoFunctionBlock)
    {
        self.gotoFunctionBlock(sender.tag);
    }
}

#pragma mark - 点击头像选择
- (void)headImageClick
{

    ZLPhotoActionSheet *ac = [[ZLPhotoActionSheet alloc] init];
    //相册参数配置，configuration有默认值，可直接使用并对其属性进行修改
    ac.configuration.maxSelectCount = 1;
    ac.configuration.maxPreviewCount = 10;
    //如调用的方法无sender参数，则该参数必传
    ac.sender = (id)[HWUtilties getCurrentVC];
    //选择回调
    [ac setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        
        [self.headButton setImage:images[0] forState:UIControlStateNormal];
        NSData *data = UIImageJPEGRepresentation(images[0],1);
        [kUserDefaults setValue:data forKey:@"HeadView"];
        [kUserDefaults synchronize];
//        WeakSelf(self)
//        [MyInfoManager modifyHeadImg:images.firstObject Sucess:^(id resopnse) {
//
//            [SVProgressHUD showSuccessWithStatus:@"修改头像成功"];
//            DISMISS_SVP(1.0f);
//
//            [weakSelf.headButton setImage:images[0] forState:UIControlStateNormal];
//
//        } fail:^(NSError *error) {
//
//        }];
        
    }];
    //调用相册
    [ac showPreviewAnimated:YES];

}

#pragma mark - 点击嗨友
- (void)gotoFriend
{
    NSLog(@"嗨友");
}

#pragma mark - 点击嗨赚等级
- (void)getMoneyClick
{
    NSLog(@"嗨等级");
}

#pragma mark - 点击充值
- (void)topUpMoney
{
    HWBookTicketViewController *vc = [[HWBookTicketViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

#pragma mark - 点击提现
- (void)getMoney
{

    HWMyWalletViewController *vc = [[HWMyWalletViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

#pragma mark - 点击开通VIP
- (void)openVip
{
    if ([current_User.isHideinfo.stringValue isEqualToString:@"1"])
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate gotoHome];
    }
    else
    {
        HWRechargeVIPViewController *vc = [[HWRechargeVIPViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - 去往个人信息
- (void)gotoPersonInfo
{
    HWPersonInformationViewController *vc = [[HWPersonInformationViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

#pragma mark - 获得数据
- (void)getMyinformationDate:(HWMyinformationModel *)model
{
    
    self.hwUserNameLabel.text = model.nickName;
    [self.hwUserNameLabel sizeToFit];
    self.hwUserIdLabel.text = [NSString stringWithFormat:@"嗨友ID:%@",model.outUserId];
    [self.hwUserIdLabel sizeToFit];
    
    if ([model.headImg isEqualToString:@""])
    {
        NSData *data = LCUserDefaultsGet(@"HeadView");
        if (data)
        {
            [self.headButton setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
        }
    }
    else
    {
         [self.headButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.headImg] forState:UIControlStateNormal];
    }
   
    
    if (/* DISABLES CODE */ (1))
    {
        self.hwVipImageView.hidden = YES;
        if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
        {
            self.hwFriendButton.hidden = NO;
            self.hwLevelButton.hidden = NO;
            self.hwFriendButton.frame = CGRectMake(CGRectGetMaxX(self.hwUserNameLabel.frame)+10*kUIScaleBasePlus,24*kUIScaleBasePlus,36*kUIScaleBasePlus,11*kUIScaleBasePlus);
            self.hwLevelButton.frame = CGRectMake(CGRectGetMaxX(self.hwFriendButton.frame)+5*kUIScaleBasePlus,17*kUIScaleBasePlus,62*kUIScaleBasePlus,20*kUIScaleBasePlus);
        }
        else
        {
            self.hwFriendButton.hidden = YES;
            self.hwLevelButton.hidden = YES;
        }
        
    }
    else
    {
        self.hwVipImageView.hidden = NO;
        [self.hwFriendButton setX:180*kUIScaleBasePlus];
        [self.hwLevelButton setX:263*kUIScaleBasePlus];
    }
    
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        self.hideView.hidden = YES;
    }
    else
    {
        self.hideView.hidden = NO;
    }
    
    [self.hwLevelButton setTitle:model.agentLevelName forState:UIControlStateNormal];
    
    self.hwBookCoinLabel.text = model.bookCoin;
    
    self.hwGoldenCoinLabel.text = model.goldCoin;
    
    if ([[model.isVip stringValue] isEqualToString:@"1"])
    {
        self.hwNormalImageView.image = [UIImage imageNamed:@""];
        self.hwEndTimeLabel.text = [NSString stringWithFormat:@"%@\n%@",@"截止日期",[HWUtilties getTime:model.readDueTime]];
    }
    else
    {
        if ([current_User.isHideinfo.stringValue isEqualToString:@"1"])
        {
            self.hwNormalImageView.image = [UIImage imageNamed:@"kanshu_haidu"];
        }
        else
        {
            self.hwNormalImageView.image = [UIImage imageNamed:@"kaitong_haidu"];
        }
        
    }
    
    if (/* DISABLES CODE */ (1))
    {
        self.hwAdvertisementImageView.image = [UIImage imageNamed:@"advertisement"];
    }

}
@end
